package common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class IoUtil
{
	/***
	 * 读取配置文件的内容
	 */
	public String readContent(InputStream is)
	{	
		StringBuilder sb = new StringBuilder();
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
			String line = "" ; 
			while( ( line = br.readLine() ) != null )
			{
				sb.append(line + "\r\n");
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return sb.toString() ;
	}
	/**
	 * 拷贝 文件
	 * @param is
	 * @param os
	 * @return
	 */
	public boolean copyFile(InputStream is,OutputStream os)
	{
		BufferedInputStream bis = new BufferedInputStream(is);
		BufferedOutputStream bos = new BufferedOutputStream(os);
		
		try
		{
			byte[] b = new byte[5 * 1024 * 1024] ; 
			int len = 0 ; 
			while( (len = bis.read(b)) != -1)
			{
				bos.write(b, 0, len);
			}
			return true ; 
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if(bis != null)
				{
					bis.close(); 
					bis = null ; 
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			
			try
			{
				if(bos != null)
				{
					bos.close(); 
					bos = null ; 
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return false ; 
	}
}
